package cn.zwt.wolf.service.program;

import cn.zwt.wolf.domain.program.OrgProgram;
import cn.zwt.wolf.repository.program.OrgProgramRepository;
import cn.zwt.wolf.util.JPQLSearchException;
import cn.zwt.wolf.util.SearchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.NoResultException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangwt on 16-2-28.
 */
@Service
public class OrgProgramService {
    @Autowired
    private OrgProgramRepository orgProgramRepository;

    @Autowired
    private SearchUtil searchUtil;
    private Logger logger = LoggerFactory.getLogger(OrgProgramService.class);

    public OrgProgram findOrgProgramById(int id){
        OrgProgram orgProgram = orgProgramRepository.findOne(id);
        return orgProgram;
    }

    public void saveOrUpdateOrgProgram(OrgProgram orgProgram){
        if (orgProgram!=null)
            orgProgramRepository.save(orgProgram);
    }

    public OrgProgram findOrgProgramByContentId(int contentId){
        OrgProgram orgProgram =null;
        if ((Integer)contentId == null){
            return orgProgram;
        }
        String hql = "from OrgProgram where contentId=:contentId";
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("contentId", contentId);
        try {
            orgProgram = (OrgProgram) searchUtil.getSingleResult(hql,map);
        } catch (JPQLSearchException e) {
            logger.error("findOrgProgramByContentId error :" + e.getMessage());
        }catch (NoResultException e){
            logger.error("findOrgProgramByContentId error :" + e.getMessage());
        }
        return orgProgram;
    }
}
